package cn.com.qtgl;

import cn.com.qtgl.oauth2.TokenManager;
import cn.com.qtgl.oauth2.TokenMetaHandlerMethodArgumentResolver;
import cn.com.qtgl.oauth2.interceptor.OAuth2Interceptor;
import cn.com.qtgl.sso.oidc.comp.interceptor.IdentificationInterceptor;
import cn.melonlib.cores.commons.comp.utils.ApplicationContextUtils;
import cn.melonlib.cores.commons.comp.utils.YamlSourceFactory;
import cn.melonlib.cores.commons.comp.web.resolvers.ApiExceptionResolver;
import cn.melonlib.cores.web.comp.interceptor.WebInfoInterceptor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.ImportResource;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.util.List;

@SpringBootApplication
@PropertySource(value = {"classpath:db.yml","classpath:fs.yml","classpath:register.yml"}, factory = YamlSourceFactory.class)
@ImportResource({"classpath:spring-context-datasource.xml"})
@ComponentScan({"cn.melonlib","cn.com.qtgl.sso","cn.com.qtgl.apis","cn.com.qtgl.apis.iam.comp","cn.com.qtgl.init.tenant","cn.melonlib.record.oper"})
@EnableCaching
//@EnableAspectJAutoProxy(proxyTargetClass = true)
public class MainBootApplication implements WebMvcConfigurer {

    public static void main(String[] args) {
        ApplicationContextUtils.setApplicationContext(SpringApplication.run(MainBootApplication.class, args));
    }

    @Resource
    private TokenManager tokenManager;

    @Resource
    private IdentificationInterceptor identificationInterceptor;

    @Resource
    private WebInfoInterceptor webInfoInterceptor;

    @Resource
    private ApiExceptionResolver apiExceptionResolver;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedHeaders("*")
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH", "OPTION");
    }

    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new OAuth2Interceptor(tokenManager)).addPathPatterns("/**");
        registry.addInterceptor(webInfoInterceptor).addPathPatterns("/**");
        registry.addInterceptor(identificationInterceptor).addPathPatterns("/provider","/api/file/upload.api","/oa/document/file/*");
//        registry.addInterceptor(new BasicInterceptor()).addPathPatterns("/provider");
    }

    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(new TokenMetaHandlerMethodArgumentResolver(tokenManager));
    }

    @Override
    public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> resolvers) {
        resolvers.add(apiExceptionResolver);
    }
    //    @Override
//    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
//        return builder.sources(TeachApplication.class);
//    }
}
